/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.kth.lwr.jeat.samples.onsitetreatment;

import se.kth.lwr.jeat.samples.onsitetreatment.producers.MacadamExtraction;
import se.kth.lwr.jeat.samples.onsitetreatment.producers.GravelAndSandExtraction;
import se.kth.lwr.jeat.samples.onsitetreatment.producers.GeoTextileProducer;
import se.kth.lwr.jeat.samples.onsitetreatment.producers.PVCPipeProduction;
import javax.measure.quantity.*;
import javax.measure.unit.NonSI;
import javax.measure.unit.SI;
import javax.measure.unit.Unit;
import org.jscience.physics.amount.Amount;
import se.kth.lwr.jeat.core.Activity;
import se.kth.lwr.jeat.core.Inventory;
import se.kth.lwr.jeat.core.AmountDataSource;
import se.kth.lwr.jeat.core.EmissionCategory;
import se.kth.lwr.jeat.utils.AmountVolume;

/**This activity includes all contributions from things that are 
 * related to a bed filter unit
 *
 * @author davidev
 */
public class BedFilter extends WasteWaterTreatmentUnit{
    
    
    public BedFilter(String name, String description) {
        super(name, description);
        setupChildActivities();
        
        
    }
    //setup all child activities for a bed filter
    private void setupChildActivities(){
        //add production of drainage pipes
        this.addChild(PVCPipeProduction.get110mmGroundInstance().getProduction(
                "drainage pipe",
                "drainage pipes in bed filter system",
                Amount.valueOf(6, SI.METER),
                lifeTimeBedFilter,
                Scenario1.systemUsers
               ));
        // add production of distribution pipes
        this.addChild(PVCPipeProduction.get110mmGroundInstance().getProduction(
                "Distribution pipe production",
                "production of drainage pipe for bed filter outflow",
                Amount.valueOf(15, SI.METER),
                lifeTimeBedFilter,
                Scenario1.systemUsers
               ));
        
        //creates a volume of macadam for distribution layer
        AmountVolume volMacadam=new AmountVolume(Amount.valueOf(15, SI.SQUARE_METRE), Amount.valueOf(0.35, SI.METER));
        //add production of distribution layer
        this.addChild(MacadamExtraction.getInstance().getProduction(
                "Macadam production",
                "Extraction of macadam for distribution layer",
                volMacadam,
                lifeTimeBedFilter,
                Scenario1.systemUsers
                ));
        //Add geo textile production
        this.addChild(GeoTextileProducer.getInstance().getProduction(
                "Geo textile",
                "Geo textile for layer separation",
                Amount.valueOf(15, SI.SQUARE_METRE),
                lifeTimeBedFilter,
                Scenario1.systemUsers
                ));
        // add production of bed filter sand
        this.addChild(GravelAndSandExtraction.getSandInstance().getProduction(
                "Filter bed sand", 
                "Filter bed sand for biological treatment",
                new AmountVolume(Amount.valueOf(30, SI.SQUARE_METRE), Amount.valueOf(0.8, SI.METER)),
                lifeTimeBedFilter,
                Scenario1.systemUsers));    
    }
    
    //here we process the wastewater comming from the wastewater unit before
    //this one
        @Override
    protected Inventory processWasteWaterInventory(Inventory inventory) {
        //calculate absolute reduction
        Amount Pin=inventory.getEmissionToWater(EmissionCategory.P);
        Amount Pred=Pin.times(P_removal_bedfilter.useValue(this));
        Amount Nin=inventory.getEmissionToWater(EmissionCategory.N);
        Amount Nred=Nin.times(N_removal_bedfilter.useValue(this));
        Amount BODin=inventory.getEmissionToWater(EmissionCategory.BOD7);
        Amount BODred=BODin.times(BOD_removal_bedfilter.useValue(this));
        
        //ADD the negative reductions to the inventory
        inventory.addEmissionToWater(EmissionCategory.P, Pred.opposite());
        inventory.addEmissionToWater(EmissionCategory.N, Nred.opposite());
        inventory.addEmissionToWater(EmissionCategory.BOD7, BODred.opposite());
       
        return inventory;   
    }
    
    //this is the last wastewater treatment unit in this scenario. Thus
    // we need to add the separated wastewater emissions to the inventory here
    @Override
    public Inventory contribute(Inventory inventory) {
        return inventory.merge(getWasteWaterInventory());
    }
    
    
    
    
    /**
     * Expected life time of bed filter sytem
     * <p/>
     * Value: 15
     * <p/>
     * Unit: year
     */
    public static final AmountDataSource<Duration> lifeTimeBedFilter = new AmountDataSource<>(
            "lifeTimeBedFilter",
            "Expected life time of bed filter sytem",
            Amount.valueOf(15, NonSI.YEAR));


    
    
    /**
     * Nitrogen removal (%) in bedfilter
     * <p/>
     * Value:50       
     * <p/>
     * Unit: %
     */
    public static final AmountDataSource<Dimensionless> N_removal_bedfilter = new AmountDataSource<>(
            "N_removal_bedfilter",
            "Nitrogen removal (%) in bedfilter",
            Amount.valueOf(50, NonSI.PERCENT));
    
    /**
     * P removal (%) in bedfilter
     * <p/>
     * Value: 50
     * <p/>
     * Unit: %
     */
    public static final AmountDataSource<Dimensionless> P_removal_bedfilter = new AmountDataSource<>(
            "P_removal_bedfilter",
            "P removal (%) in bedfilter",
            Amount.valueOf(50, NonSI.PERCENT));
    
    /**
     * BOD removal (%) in bedfilter
     * <p/>
     * Value: 90
     * <p/>
     * Unit: %
     */
    public static final AmountDataSource<Dimensionless> BOD_removal_bedfilter = new AmountDataSource<>(
            "BOD_removal_bedfilter",
            "BOD removal (%) in bedfilter",
            Amount.valueOf(90, NonSI.PERCENT));
    

    

    


    
    
}
